package source.view;

import java.awt.*;
import javax.swing.text.*;
import javax.swing.*;
/**
 * Viewport that provides a textual commentary on events during the gameplay
 * @author McDonald's Farm
 *
 */
public class ConsoleViewport extends JScrollPane {
	private JTextPane consoleTextArea;
	private AbstractDocument doc;
	private SimpleAttributeSet attrs;
	
	public ConsoleViewport(){
		super();
		consoleTextArea = new JTextPane();
		consoleTextArea.setEditable(false);
        StyledDocument styledDoc = consoleTextArea.getStyledDocument();
        if (styledDoc instanceof AbstractDocument) {
            doc = (AbstractDocument)styledDoc;
        }
        
        attrs = new SimpleAttributeSet();
        StyleConstants.setFontFamily(attrs, "SansSerif");
        StyleConstants.setFontSize(attrs, 16);
		setViewportView(consoleTextArea);
	}
	/**
	 * Appends the string to the text area.
	 * @param str
	 */
	public void writeToConsole(String str, Color c)
	{
		consoleTextArea.setCaretPosition(0);
		StyleConstants.setForeground(attrs, c);
        try {
		  doc.insertString(0,str + "\n", attrs);
        } catch (BadLocationException ble) {
        	System.err.println("Couldn't insert initial text.");
        }
		//consoleTextArea.insert(str + "\n", 0);
	}
	
}
